package may.naing.sysadmin.daoimpl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sql.RowSet;

import may.naing.sysadmin.beans.UserMemberBean;
import may.naing.sysadmin.dao.*;
import may.naing.sysadmin.cmd.*;

public class MemberDAOImpl implements IMember  {

	protected Connection Conn;
	private static final MemberSqlCommand cmdMember = new MemberSqlCommand();
	
	public MemberDAOImpl(final Connection conn)
	{
		this.Conn = conn;
	}

	@Override
	public int insertMember(UserMemberBean memberBean) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public boolean deleteMember(int intMemberID) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public UserMemberBean findMember(int intMemberID) {
		// TODO Auto-generated method stub
		return null;
	}

	
	
	@Override
	public UserMemberBean loginMember(String strUName, String strPassword) {
		UserMemberBean memberBean = new UserMemberBean();
		String strCmd = cmdMember.loginMember(strUName, strPassword);
		try {
			Statement stmt = Conn.createStatement();
			ResultSet rs= stmt.executeQuery(strCmd);
			if (rs.next()){
				memberBean.setFirst_name(rs.getString("f_name"));
				memberBean.setLast_name(rs.getString("last_name"));
				memberBean.setGender(rs.getBoolean("gender"));
				memberBean.setEmail(rs.getString("email"));
				memberBean.setDob(rs.getDate("dob"));				
			}
			return memberBean;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return null;
	}

	@Override
	public boolean updateMember(UserMemberBean memberBean) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public RowSet selectMemberRS() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ArrayList<UserMemberBean> selectMember() throws SQLException {
		ArrayList<UserMemberBean> memberlist = new ArrayList<UserMemberBean>();
		String selectSql = "SELECT * From Member";
				
		try
		{			
			Statement st = Conn.createStatement();
			Logger.getLogger(getClass().getName()).log(Level.INFO,"Executing Select:"+ selectSql);
			ResultSet rs = st.executeQuery(selectSql);
			 while (rs.next()) {
				 UserMemberBean member = new UserMemberBean();
	             member.setFirst_name(rs.getString("first_name"));
	             member.setLast_name(rs.getString("last_name"));
	             member.setEmail(rs.getString("email"));
	             member.setUname(rs.getString("uname"));
	             member.setPassword(rs.getString("password"));
	             member.setDob(rs.getDate("dob"));
	             member.setGender(rs.getBoolean("gender"));
	             memberlist.add(member);
	         }
			st.close();
			
		}catch(SQLException sqlex)
		{						
			throw sqlex;
		}
		finally
		{
			try
			{
				Conn.close();
			}catch(Exception e){}
			
		}		
		
		return memberlist;
	}

}
